package com.juttec.freshbird.pet;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioGroup;

import com.juttec.freshbird.pet.luntantiezi.BaseTiezi;
import com.juttec.freshbird.pet.luntantiezi.Jinghuatie;

import java.util.ArrayList;

public class LuntanActivity extends Activity {

    private RadioGroup radioGroup;
    private ViewPager vp;
    private ArrayList<BaseTiezi> baseTiezis;
    private BaseTiezi mBaseTiezi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luntan);
        init();
    }

    private void init() {
        radioGroup = (RadioGroup) findViewById(R.id.rg_luntan_group);
        vp = (ViewPager) findViewById(R.id.vp_activity_luntan);
        baseTiezis = new ArrayList<BaseTiezi>();
        for(int i=0;i<3;i++){
            Jinghuatie jinghuatie = new Jinghuatie(LuntanActivity.this);
            baseTiezis.add(jinghuatie);
        }
        initData();
    }

    private void initData() {
        vp.setAdapter(new LuntantieziAdapter());

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                baseTiezis.get(position).initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        baseTiezis.get(0).initData();
        radioGroup.check(R.id.rb_zuixinfabu);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_zuixinfabu:
                        vp.setCurrentItem(0,false);
                    break;
                    case R.id.rb_jinghuatie:
                        vp.setCurrentItem(1,false);
                        break;
                    case R.id.rb_putongtie:
                        vp.setCurrentItem(2,false);
                        break;
                }
            }
        });
    }

    class LuntantieziAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            mBaseTiezi = baseTiezis.get(position);
            container.addView(mBaseTiezi.mRootView);
            return mBaseTiezi.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
